package com.zy.project.system.online.manager.impl;

import com.google.common.collect.Lists;
import com.zy.core.common.security.redis.RedisManager;
import com.zy.core.common.security.redis.RedisSessionDAO;
import com.zy.core.common.security.redis.SerializeUtils;
import com.zy.core.common.security.session.OnlineSessionDAO;
import com.zy.core.data.jpa.support.entity.page.Pagination;
import com.zy.core.data.jpa.support.manager.impl.BaseManagerImpl;
import com.zy.core.data.jpa.support.repository.BaseRepository;
import com.zy.project.system.online.entity.OnlineSession;
import com.zy.project.system.online.entity.UserOnline;
import com.zy.project.system.online.manager.UserOnlineManager;
import com.zy.project.system.online.repository.UserOnlineRepository;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 在线回话相关
 * Package: com.zy.core.common.security.manager.impl
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-11-4 13:27
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
@Service
@Transactional(readOnly=true)
public class UserOnlineManagerImpl extends BaseManagerImpl<UserOnline,String> implements UserOnlineManager {

    @Autowired
    private UserOnlineRepository userOnlineRepository;

    @Autowired
    private RedisManager redisManager;

    private static final String SESSION_KEY_PREFIX = "shiro_redis_session:";

    @Autowired
    private RedisSessionDAO sessionDAO;

    @Override
    public BaseRepository<UserOnline, String> getDao() {
        return this.userOnlineRepository;
    }


    @Override
    @Transactional(readOnly=false)
    public void forceLoginoutBySessionId(String sessionId) {

    }

    @Override
    public List<UserOnline> findByExpired(Date date) {
        return userOnlineRepository.findByExpired(date);
    }

    @Override
    public Collection<OnlineSession> findAllOnlineSession() {
        Set<OnlineSession> sessions = new HashSet<OnlineSession>();

        Set<byte[]> keys = redisManager.keys(SESSION_KEY_PREFIX + "*");
        if(keys != null && keys.size()>0){
            for(byte[] key:keys){
                OnlineSession s = (OnlineSession) SerializeUtils.deserialize(redisManager.get(key));
                sessions.add(s);
            }
        }
        return sessions;
    }

    @Transactional(readOnly=false)
    @Override
    public void deleteByIds(String... ids){
        for (String id : ids) {
            byte[] sessionKey = SerializeUtils.serialize(SESSION_KEY_PREFIX + id);
            Session session = sessionDAO.readSession(id);
            if(session != null){
                sessionDAO.delete(session);
            }
            redisManager.del(sessionKey);
        }
    }

    @Override
    public Pagination<OnlineSession> findAllOnlineSession(int pageNo, int pageSize) {
        Collection<OnlineSession> allOnlineSession = this.findAllOnlineSession();
        List<OnlineSession> sessions = Lists.newArrayList(allOnlineSession);
        Long total = new Long(sessions.size());
        Pagination<OnlineSession> pagination = new Pagination<OnlineSession>(pageNo, pageSize,
                total.intValue(), sessions);
        return pagination;
    }
}
